package com.our.sys.project1.service;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.our.set.service.HzService;
import com.our.sys.project1.dao.TofficesDao;
import com.our.sys.project1.entity.Toffices;

@Service
@Transactional
public class TofficesService {

    @Autowired
    private TofficesDao tofficesDao;
    
    @Autowired
    private HzService hzService;
    
    private static Logger logger = LoggerFactory.getLogger(TofficesService.class);
    
    @Transactional(readOnly = true)
    public Page<Toffices> query(Page<Toffices> page, List<PropertyFilter> filters) {
        Page<Toffices> curPage = tofficesDao.findPage(page, filters);
        return curPage;
    }

    @Transactional(readOnly = true)
    public Toffices queryById(Long id) {
        return tofficesDao.findOneBy("id", id);
    }

    @Transactional(readOnly = true)
    private Long queryCountByName(String name) {
        Long result = tofficesDao.findUnique("select count(a) from Toffices a where a.name = ?", name);
        if (null == result) {
            return 0L;
        } else {
            return result;
        }
    }

    @Transactional(readOnly = true)
    private Long queryCountByCode(String code) {
        Long result = tofficesDao.findUnique("select count(a) from Toffices a where a.code = ?", code);
        if (null == result) {
            return 0L;
        } else {
            return result;
        }
    }

    public void save(Toffices usedType) throws Exception {
        try {
            Map<String, String> map = hzService.getMemo(usedType.getName());
            usedType.setPy(map.get("py"));
            usedType.setWb(map.get("wb"));
            if (null != usedType.getId()) {
                Toffices getToffices = this.queryById(usedType.getId());

                if (queryCountByName(getToffices.getName()).compareTo(1L) > 0) {
                    throw new Exception("已经存在这个科室名称！");
                }
                
                if (queryCountByCode(getToffices.getCode()).compareTo(1L) > 0) {
                    throw new Exception("已经存在这个科室代码！");
                }

                getToffices.setCode(usedType.getCode());
                getToffices.setName(usedType.getName());
                getToffices.setPy(usedType.getPy());
                getToffices.setWb(usedType.getWb());
                tofficesDao.save(getToffices);
            } else {

                if (queryCountByName(usedType.getName()).compareTo(0L) > 0) {
                    throw new Exception("已经存在这个科室名称！");
                }
                
                if (queryCountByCode(usedType.getCode()).compareTo(0L) > 0) {
                    throw new Exception("已经存在这个科室代码！");
                }
                tofficesDao.save(usedType);
            }

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void delete(Long id) throws Exception {
        try {
            tofficesDao.delete(id);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }
}
